package com.example.rabbitmqretry.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    @Value("${mq.queueBinding.queue}")
    private String queueName;
    @Value("${mq.queueBinding.exchange.name}")
    private String exchangeName;
    @Value("${mq.queueBinding.key}")
    private String key;

    private static String dlxTopicExchange = "dlx_topic_exchange_name";
    private static String dlxQueue = "dlx_queue_name";
    private static String dlxRoutingKey = "dlx_routing_key";

    @Autowired
    RabbitTemplate rabbitTemplate;

    // 定义死信队列、死信交换机、死信队列绑定关系
    @Bean
    Queue dlxQueue(){
        return new Queue(dlxQueue);
    }
    @Bean
    TopicExchange dlxTopicExchange(){
        return new TopicExchange(dlxTopicExchange);
    }
    @Bean
    Binding dlxBinding(){
        return BindingBuilder.bind(dlxQueue()).to(dlxTopicExchange()).with(dlxRoutingKey);
    }

    // 定义业务队列、业务交换机、业务队列绑定关系
    @Bean
    Queue payQueue(){
        Map<String, Object> args = new HashMap<>();
        //设置死信交换机
        args.put("x-dead-letter-exchange", dlxTopicExchange);
        //设置死信 routing_key
        args.put("x-dead-letter-routing-key", dlxRoutingKey);
        return new Queue(queueName,true,false,false,args);
    }

    @Bean
    TopicExchange payTopicExchange(){
        return new TopicExchange(exchangeName,true,false);
    }

    @Bean
    Binding payBinding(){
        return BindingBuilder.bind(payQueue()).to(payTopicExchange()).with(key);
    }
}
